/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.nonmember;

import entity.Product;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Paginator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.ProductServiceLocal;

/**
 *
 * @author SONY
 */
public class SearchProductByCategoryStrutsAction extends org.apache.struts.action.Action {
    
    @EJB ProductServiceLocal productServiceLocal;    

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    static int N = 0;
    static int n = 0;
    static int nPages = 0;
    static int currentpage = 0;    

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List<Product> lstProducts = new ArrayList<Product>();
        try {
            //request.setCharacterEncoding("UTF-8");
            String cid = request.getParameter("cid");
            if(request.getAttribute("cid") != null) {
                cid = request.getAttribute("cid").toString();
            }
            ServletContext context = servlet.getServletContext();
            productServiceLocal = (ProductServiceLocal) context.getAttribute("productServiceLocal");

            lstProducts = productServiceLocal.getAllProducts_byCategoryID(cid);
            
            n = lstProducts.size();
            
            N = 6;
            if( n % N == 0) {
                nPages = (int)n/N;
                System.out.println("nPages_0 " + nPages);

            } else {	
                nPages = (int)Math.ceil((double)(n)/N);
                System.out.println("nPages_1 "+ ((double)(n)/N) + " : " + nPages);

            }
            
            String current = request.getParameter("currentpage");
            if(current == null){
                currentpage=0;
            }else{
                currentpage=Integer.parseInt(current);
            }

            System.out.print("current page" + currentpage);

            HttpSession session=request.getSession();
            session.setAttribute("nPages", nPages);
            
            Paginator paging = new Paginator();
            List<Product> sublist = paging.getSubList(currentpage, N, n, lstProducts);
                
            request.setAttribute("products_search", sublist);
            request.setAttribute("cid", cid);
            return mapping.findForward(SUCCESS);
            
        } catch (Exception e) {
            System.err.println(e);
        }
        request.setAttribute("products_search", lstProducts);
        return mapping.findForward(SUCCESS);
    }
}
